package com.springboot.util.http;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @description:
 * @author: zyj
 * @create: 2020-07-30 14:26
 **/
public class ResponseBuilder {
    public static String OK_CODE = "200";
    public static String OK_MSG = "成功";
    public static String ERR_CODE = "500";
    public static String ERR_MSG = "失败";

    public static  <T> ResultResponse<T> success(){
        return getResponse(OK_CODE, OK_MSG, null);
    }

    public static <T> ResultResponse<T> successData(Object data){
        return getResponse(OK_CODE, OK_MSG, data);
    }

    public static  <T> ResultResponse<T> successMsg(String message){
        return getResponse(OK_CODE, message, null);
    }


    public static <T> ResultResponse<T> successMsgData(String message, Object data){
        return getResponse(OK_CODE, message, data);
    }

    public static <T> ResultResponse<T>  errorMessage(String code, String message){
        return getResponse(code, message, null);
    }


    public static  <T> ResultResponse<T>  errorMessage(String code,String message, Object object){
        return  getResponse(code, message, object);
    }


    public static <T> ResultResponse<T> getResponse(String code, String message, Object object){
        ResultResponse<T> resultResponse = new ResultResponse<T>();
        resultResponse.setCode(StringUtils.isEmpty(code) ? ERR_CODE : code);
        resultResponse.setMsg(StringUtils.isEmpty(message) ? ERR_MSG : message);
        if(null == object){
            object = "";
        }
        resultResponse.setData(object);
        return resultResponse;
    }

    public static <T> ResponseEntity<ResultResponse<T>> errorHeaderMessage(String code, String message){
        return successHeaderMsgData(code, message, null, null);
    }

    public static  <T> ResponseEntity<ResultResponse<T>> successHeaderData(Object data, MultiValueMap<String, String> headers){
        return successHeaderMsgData(OK_CODE, OK_MSG, data, headers);
    }

    public static  <T> ResponseEntity<ResultResponse<T>> successHeaderMsg(String message, MultiValueMap<String, String> headers){
        return successHeaderMsgData(OK_CODE, message, null, headers);
    }

    public static <T> ResponseEntity<ResultResponse<T>> successHeaderMsgData(String code, String message, Object data, MultiValueMap<String, String> headers){
        return  new ResponseEntity<>(getResponse(code, message, data), headers, HttpStatus.OK);
    }

    public static ResponseEntity<byte[]> downFile(String fileName, InputStream is, String fileType) throws Exception {
        byte[] body =  new byte[is.available()];
        is.read(body);
        return downFile(fileName, body, fileType);
    }


    public static ResponseEntity<byte[]> downFile(String fileName, byte[] body, String fileType) throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attchement;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8)+"."+fileType);
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(body, headers, statusCode);
        return responseEntity;
    }

}
